package com.bwsoft.api.action;

import java.io.IOException;
import java.io.PrintWriter;
import java.util.Calendar;
import java.util.HashMap;
import java.util.Map;

import net.sf.json.JSONObject;

import org.apache.log4j.Logger;

import com.bwsoft.admin.domain.News;
import com.bwsoft.api.pojo.ResponsePojo;
import com.bwsoft.api.util.Constant;
import com.bwsoft.util.Base64Util;
import com.bwsoft.util.DesUtil;

public class NewsAction extends ApiBaseAction<NewsAction> {
	private static Logger LOG = Logger.getLogger(NewsAction.class);

	private Integer category;
	private Integer newsId;
	private News news;

	/**
	 * 2.2.1资讯类别
	 * 
	 * @return
	 * @throws Exception 
	 */
	public void  getNewsCategory() throws Exception {
		
		LOG.info("=================================开始调用【2.2.1资讯类别 getCategory】获取资讯类别==============================");
		//得到调用接口开始时的毫秒数
		beginTimestamp=Calendar.getInstance().getTimeInMillis();
		
		// 调用service方法得到要返回的数据对象
		ResponsePojo rp = newsService.getCategory();
		responseJson.put("errcode", rp.getErrcode());
		responseJson.put("errmsg", rp.getErrmsg());
		responseJson.put("data", rp.getData());
		
		finishMethod();
		LOG.info("=================================调用结束【2.2.1资讯类别 getCategory】获取资讯类别==============================");
	}
	
	/**
	 * 2.2.2资讯列表
	 * news_getList.action?r=xivhjhON/WzUfhwgaWk3HsL8XzYWQvHw
	 * @return
	 * @throws Exception 
	 * @throws IOException 
	 */
	@SuppressWarnings("unchecked")
	public void getList() throws IOException, Exception {
		LOG.info("=================================开始调用【2.2.2资讯列表 getList】获取资讯类别==============================");
		
		LOG.info("传入参数r="+super.getR());
		
		if(r==null||r.trim().equals("")){
			responseJson.put("errcode", 1);
			responseJson.put("errmsg", "r值不能为空");
			finishMethod();
			LOG.info("=======================================传入参数r为空，提前结束");
		}
		
		Map<String, Object> parameMap= reflexParameters();
		
		if(!parameMap.isEmpty()&&parameMap.containsKey("category")) this.setCategory(Integer.parseInt((String) parameMap.get("category")));
		if(!parameMap.isEmpty()&&parameMap.containsKey("page")) setPage(Integer.parseInt((String) (parameMap.get("page"))));
		
		//得到调用接口开始时的毫秒数
		beginTimestamp=Calendar.getInstance().getTimeInMillis();
		
		// 调用service方法得到要返回的数据对象
		//LOG.info("传入参数 category=" + category + " ,page=" + page);
		
		//如果page为null则赋值默认值取第一页
		page=(page==null||page<=0)?1:page;
		
		//判断category是否为空，为空则返回
		if (category == null ) {
			responseJson.put("errcode", 1);
			responseJson.put("errmsg", "传入参数category不能为空");
			
			finishMethod();
			
			LOG.info("=================================调用结束【2.2.2资讯列表 getList】获取资讯类别==============================");
		}
		
		//调用取数流程
		LOG.info("开始调用取数流程");
		ResponsePojo rp = newsService.getList(category, page);
		LOG.info("调用取数流程结束");
		responseJson.put("errcode", rp.getErrcode());
		responseJson.put("errmsg", rp.getErrmsg());
		responseJson.put("data", rp.getData());
		
		finishMethod();
		
		LOG.info("=================================调用结束【2.2.2资讯列表 getList】获取资讯类别==============================");
	}
	
	
	/**
	 * 跳转到New页面
	 * @return
	 */
	public String page(){
		LOG.info("=================================开始调用【news page】==============================");
		
		LOG.info("传入参数newsId="+newsId);
		
		news=newsService.findNews(newsId);
		
		LOG.info("根据条件查找到news"+news);
		LOG.info("=================================调用结束【news page】==============================");
		return news==null?"page404":"page";
	}
	

	public Integer getCategory() {
		return category;
	}

	public void setCategory(Integer category) {
		this.category = category;
	}

	public Integer getNewsId() {
		return newsId;
	}

	public void setNewsId(Integer newsId) {
		this.newsId = newsId;
	}

	public News getNews() {
		return news;
	}

	public void setNews(News news) {
		this.news = news; 
	}

}
